package com.example.musicserver.controller;

import com.example.musicserver.config.ResponseBodyMessage;
import com.example.musicserver.constant.Constants;
import com.example.musicserver.model.User;
import com.example.musicserver.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-01-15
 * Time: 12:16
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @RequestMapping("/login")
    public ResponseBodyMessage<User> login(@RequestParam String userName
            , @RequestParam String password , HttpServletRequest request){
        User loginUser = new User();
        loginUser.setUserName(userName);
        loginUser.setPassword(password);
        User userInfo = userService.selectByName(loginUser);
        if(userInfo == null){
            return new ResponseBodyMessage<>(-1,"用户名或者密码错误", userInfo);
        }else{
            if(!bCryptPasswordEncoder.matches(password,userInfo.getPassword())){
                return new ResponseBodyMessage<>(-1,"用户名或者密码错误",userInfo);
            }
            request.getSession().setAttribute(Constants.USERINFO_SESSION_KEY, userInfo);
            return new ResponseBodyMessage<>(0,"登录成功", userInfo);
        }
    }


}
